﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBUtlity;
using Models;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    public class PJNodeService
    {
        #region 新建
        public int InsertInfo(PJNode PJNode,string tableName)
        {
            List<SqlParameter> param = new List<SqlParameter>();
            string sql = "insert into "+tableName;
            sql+="(WorkParentId,PJId,Number,NodeName,";
            if (tableName == "PJNodesTemp")
            {
                sql += "Reason,";
                param.Add(new SqlParameter("@Reason", PJNode.Reason));
            }
            sql += "CompleteDate,Content)";
            sql += " values(@WorkParentId,@PJId,@Number,@NodeName,";
            if (tableName == "PJNodesTemp")
            {
                sql += "@Reason,";
            }
            sql += "@CompleteDate,@Content)";
            param.Add(new SqlParameter("@WorkParentId",PJNode.WorkParentId));
            param.Add(new SqlParameter("@PJId",PJNode.PJId));
            param.Add(new SqlParameter("@Number", PJNode.Number));
            param.Add(new SqlParameter("@NodeName", PJNode.NodeName));
            param.Add(new SqlParameter("@CompleteDate", PJNode.CompleteDate));
            param.Add(new SqlParameter("@Content", PJNode.Content));
            return SQLHelper.Update(sql, param.ToArray());
        }
        #endregion

        #region 更改
        /// <summary>
        /// 根据工作流号更改项目主表数据
        /// </summary>
        /// <param name="PJMains"></param>
        /// <returns></returns>
        public int UpdateInfo(PJNode PJNode, string tableName, string pjId)
        {
            List<SqlParameter> param = new List<SqlParameter>();
            string sql = "update " + tableName;
            sql += " set CompleteDate=@CompleteDate,";
            sql += "Content=@Content";
            if (pjId != null)
            {
                if(tableName=="PJNodesTemp")
                {
                    sql += ",Statues=@Statues,Reason=@Reason where PJId=@PJId and Number=@Number";
                    param.Add(new SqlParameter("@PJId", pjId));
                    param.Add(new SqlParameter("@Reason", PJNode.Reason));
                    param.Add(new SqlParameter("@Statues", "0"));
                }
                else
                {
                    sql += " where PJId=@PJId and Number=@Number";
                    param.Add(new SqlParameter("@PJId", pjId));
                }
            }
            else
            {
                sql += ",Statues=@Statues where WorkParentId=@WorkParentId and Number=@Number";
                param.Add(new SqlParameter("@WorkParentId", PJNode.WorkParentId));
                param.Add(new SqlParameter("@Statues", "0"));
                
            }
            param.Add(new SqlParameter("@Number", PJNode.Number));
            param.Add(new SqlParameter("@CompleteDate", PJNode.CompleteDate));
            param.Add(new SqlParameter("@Content", PJNode.Content));
            
            return SQLHelper.Update(sql, param.ToArray());
        }
        /// <summary>
        /// 根据工作流号更改项目主表状态
        /// </summary>
        /// <param name="pjId"></param>
        /// <param name="afterStatues"></param>
        /// <returns></returns>
        public int UpdateInfo(int workParentId, string afterStatues, string tableName)
        {
            string sql = "update " + tableName;
            sql += " set Statues=@Statues";
            sql += " where WorkParentId=@WorkParentId";
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@WorkParentId",workParentId),
                new SqlParameter("@Statues",afterStatues)
            };
            return SQLHelper.Update(sql, param);
        }
        #endregion

        #region 查询
        /// <summary>
        /// 查询是否已新建过PJid
        /// </summary>
        /// <param name="pjId"></param>
        /// <returns></returns>
        public int GetIsNoPJId(string pjId,string tableName)
        {
            string sql = "select count(*) from " + tableName;
            sql += " where PJId=@PJId";
            return Convert.ToInt32(SQLHelper.GetSingleResult(sql,new SqlParameter[]{new SqlParameter("PJId",pjId)}));
        }
        /// <summary>
        /// 根据workParentId或pjid查询信息
        /// </summary>
        /// <param name="workParentId"></param>
        /// <param name="pjId"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public List<PJNode> GetPJNodeInfo(int workParentId, string pjId, string tableName)
        {
            string sql = "select * from " + tableName + " where 1=1";
            SqlParameter[] param = null;
            if (pjId == null)
            {
                sql += " and WorkParentId=@WorkParentId order by Number";
                param = new SqlParameter[]
                {
                    new SqlParameter("@WorkParentId",workParentId)
                };
            }
            else
            {
                sql += " and PJId=@PJId order by Number";
                param = new SqlParameter[]
                {
                    new SqlParameter("@PJId",pjId)
                };
            }
            SqlDataReader objReader = SQLHelper.GetReader(sql, param);
            List<PJNode> list = new List<PJNode>();
            while(objReader.Read())
            {
                PJNode node = new PJNode()
                {
                    WorkParentId = Convert.ToInt32(objReader["WorkParentId"]),
                    PJId=objReader["PJId"].ToString(),
                    Number=Convert.ToInt32(objReader["Number"]),
                    NodeName = objReader["NodeName"].ToString(),
                    CompleteDate = Convert.ToDateTime(objReader["CompleteDate"]),
                    Content=objReader["Content"].ToString(),
                    Statues=objReader["Statues"].ToString()
                };
                if(tableName=="PJNodesTemp")
                {
                    node.Reason = objReader["Reason"].ToString();
                }
                list.Add(node);
            }
            objReader.Close();
            return list;
        }
        /// <summary>
        /// 获取模板信息
        /// </summary>
        /// <returns></returns>
        public  List<PJNode> GetPJNodeMould()
        {
            string sql = "select * from PJNodeMoulds";
            List<PJNode> list = new List<PJNode>();
            SqlDataReader objReader = SQLHelper.GetReader(sql);
            while(objReader.Read())
            {
                list.Add(new PJNode
                {
                    Number = Convert.ToInt32(objReader["Number"]),
                    NodeName = objReader["NodeName"].ToString(),
                    Content = objReader["StageName"].ToString()
                });
            }
            objReader.Close();
            return list;
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除节点数据
        /// </summary>
        /// <param name="pjId"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public int DeletePJNode(int workParentId,string pjId,string tableName)
        {
            string sql = "delete from " + tableName;
            SqlParameter[] param = null;
            if(pjId==null)
            {
                sql += " where WorkParentId=@WorkParentId";
                param = new SqlParameter[]
                {
                    new SqlParameter("@WorkParentId",workParentId)
                };
            }
            else
            {
                sql += " where PJId=@PJId";
                param = new SqlParameter[]
                {
                    new SqlParameter("@PJId",pjId)
                };
            }
            
           
            return SQLHelper.Update(sql, param);
        }
        #endregion

    }
}
